<?php
namespace app\decorate\model;
use app\BaseModel;
use think\facade\Db;



class IndexNews extends BaseModel
{
    //定义表名 文章表
    protected $name = 'longbing_card_decorate_news';


    protected $append = [
      'up_page',
      'next_page'
    ];
    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '=', $status);
    }
    /**
     * User: chenniang
     * Date: 2019-10-21 15:56
     * @param $value
     * @param $data
     * @return mixed
     * descption:上一页
     */
    public function getUpPageAttr($value,$data){
        $dis[] = ['uniacid','=',$data['uniacid']];
        $dis[] = ['sort','>=',$data['sort']];
        $id    = $this->where($dis)->status(1)->order('sort desc')->column('id');
        $id    = array_values($id);
        $key   = array_search($data['id'],$id)-1;
        $page  = key_exists($key,$id)?$id[$key]:'';
        return $page;
    }
    /**
     * User: chenniang
     * Date: 2019-10-21 15:56
     * @param $value
     * @param $data
     * @return mixed
     * descption:下一页
     */
    public function getNextPageAttr($value,$data){
        $dis[] = ['uniacid','=',$data['uniacid']];
        $dis[] = ['sort','<=',$data['sort']];
        $id    = $this->where($dis)->status(1)->order('sort desc')->column('id');
        $id    = array_values($id);
        $key   = array_search($data['id'],$id)+1;

        $page  = key_exists($key,$id)?$id[$key]:'';
        return $page;
    }

    /**
     * User: chenniang
     * Date: 2019-10-12 14:23
     * @param $value
     * @return array
     * descption:转换图片
     */
    public function getCoverAttr($value){
        $data = transImagesOne([$value],['cover']);
        return !empty($data[0])?$data[0]:[];
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 文章列表
     */
    public function newsList($dis,$page = 10){
        $data = $this->where($dis)->status(1)->order('sort desc')->paginate($page)->toArray();
        return $data;
    }

    /**
     * @param $dis
     * @param $data
     * @return AdminCate
     * 编辑文章
     */
    public function newsUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;

    }
    /**
     * @param $dis
     * @return AdminCate
     * 文章详情
     */
    public function newsInfo($dis){
        $data = $this->where($dis)->status(1)->find();
        $data['content1'] = getimgsV2($data['content']);
        return !empty($data)?$data->toArray():$data;
    }




}